package com.ming_za.sokoban.block;

import com.ming_za.sokoban.network.NetworkUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.phys.BlockHitResult;

/**
 * 箱子方块
 */
public class BoxBlock extends Block{

    public static final BooleanProperty HOMING = BooleanProperty.create("homing");

    public BoxBlock(Properties properties) {
        super(properties);
        registerDefaultState(this.defaultBlockState().setValue(HOMING,false));
    }

    @Override
    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
        super.createBlockStateDefinition(builder);
        builder.add(HOMING);
    }

    @Override
    public InteractionResult use(BlockState blockState, Level level, BlockPos pos, Player player, InteractionHand p_60507_, BlockHitResult hitResult) {

        Direction direction = hitResult.getDirection();

        //检测点击的方向，向相反方向移动
        switch (direction.getName()) {
            case "east":
                //点击方向为“东”

                if (checkMove(level,pos.west())) {
                    blockMove(level,blockState,pos,pos.west(),player);
                } else {
                    if (level.isClientSide) {
                        unMove(player);
                    }

                }

                break;
            case "west":
                //点击方向为“西”

                if (checkMove(level,pos.east())) {
                    blockMove(level,blockState,pos,pos.east(),player);
                } else {
                    if (level.isClientSide) {
                        unMove(player);
                    }

                }
                break;
            case "north":
                //点击方向为“北”

                if (checkMove(level,pos.south())) {
                    blockMove(level,blockState,pos,pos.south(),player);
                } else {
                    if (level.isClientSide) {
                        unMove(player);
                    }

                }
                break;
            case "south":
                //点击方向为“南”

                if (checkMove(level,pos.north())) {
                    blockMove(level,blockState,pos,pos.north(),player);
                } else {
                    if (level.isClientSide) {
                        unMove(player);
                    }

                }
                break;
            case "up":
                //点击方向为“上”

                if (checkMove(level,pos.below())) {
                    blockMove(level,blockState,pos,pos.below(),player);
                } else {
                    if (level.isClientSide) {
                        unMove(player);
                    }

                }
                break;
            case "down":
                //点击方向为“下”

                if (checkMove(level,pos.above())) {
                    blockMove(level,blockState,pos,pos.above(),player);
                } else {
                    if (level.isClientSide) {
                        unMove(player);
                    }

                }
                break;
        }

        return InteractionResult.SUCCESS;
    }

    /**
     * 当pos是空气方块（air）时返回true
     */
    private boolean checkMove(Level level , BlockPos pos){
        return Block.getId(level.getBlockState(pos)) == 0;
    }

    /**
     * 将方块移走并刷新原方块周围的方块状态
     * @param old_pos 原方块位置
     * @param pos 移动位置
     */
    private void blockMove(Level level,BlockState blockState,BlockPos old_pos ,BlockPos pos ,Player player){
        if (!level.isClientSide){
            level.setBlock(pos, blockState.setValue(HOMING,false), 1);
            level.setBlock(old_pos, Block.stateById(0), 0);
            updateNeighbor(level, old_pos);
            NetworkUtils.getInstance().s2c_clientUpdate(pos,old_pos,player);

        }else {
            updateBlock(pos,old_pos);
        }

    }

    //当方块无法移动时
    private void unMove(Player player){
        player.sendMessage(Component.nullToEmpty("此方向无法推动!"),player.getUUID());
    }

    //更新一次周边方块，避免产生方块状态不刷新的情况
    public void updateNeighbor(Level level,BlockPos pos){
        level.updateNeighborsAt(pos,this);
    }

    //网络更新
    public void updateBlock(BlockPos pos, BlockPos rPos){

        Level level = Minecraft.getInstance().level;
        assert level != null;
        level.setBlock(pos,level.getBlockState(rPos).setValue(HOMING,false),1);
        level.setBlock(rPos,Block.stateById(0),0);


    }
}
